package mars.game.core;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MarsView extends SurfaceView implements SurfaceHolder.Callback, Runnable
{
	
	public static final int FPS = 200;
	Typeface typeface = Typeface.DEFAULT;
	
	Point mPaint = new Point();
	
	// long systemTime = System.currentTimeMillis();
	// long lastFrameDuration = 0;
	// long lastFrameStartTime = systemTime;
	
	private long roundStartTime = -1;
	private long roundDurationTime;
	
	private long frameStartTime;
	private long frameDurationTime;
	private short frameCounter;
	private short fps;
	
	// public MarsScreen currGameScreen;
	
	// 控制循环
	// boolean mbLoop = false;
	
	// 定义SurfaceHolder对象
	SurfaceHolder mSurfaceHolder = null;
	
	private MarsRuntime runtime;
	
	public MarsView(Context context)
	{

		super(context);
		
		// 实例化SurfaceHolder
		mSurfaceHolder = this.getHolder();
		
		// 添加回调
		mSurfaceHolder.addCallback(this);
		this.setFocusable(true);
		runtime = MarsRuntime.getRuntime();
		runtime.init(this, (Activity) context);
		// mbLoop = true;
	}
	
	// 在surface的大小发生改变时激发
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
	{

	}
	
	// 在surface创建时激发
	public void surfaceCreated(SurfaceHolder holder)
	{

		// 开启绘图线程
		new Thread(this).start();
	}
	
	// 在surface销毁时激发
	public void surfaceDestroyed(SurfaceHolder holder)
	{

		// 停止循环
		this.runtime.isAppRunning = false;
	}
	
	// 绘图循环
	public void run()
	{

		while (this.runtime.isAppRunning)
		{
			
			// synchronized (mSurfaceHolder)
			// {
			if (runtime.getCurScreen() != null)
			{
				// 锁定画布，得到canvas
				Canvas canvas = mSurfaceHolder.lockCanvas();
				
				// if (mSurfaceHolder == null || canvas == null)
				// {
				// return;
				// }
				
				roundDurationTime = System.currentTimeMillis() - roundStartTime;
				roundStartTime = System.currentTimeMillis();
				// int curKeyState = this.getKeyState();
				// int curKeyPressed = this.getKeyPressed();
				
				this.runtime.upDataTime(roundStartTime);
				
				this.runtime.input();
				this.runtime.tick();
				this.runtime.draw(canvas);
				
				if (roundDurationTime < MarsRuntime.MAIN_CYCLE_TIME && MarsRuntime.TOGGLE_FPS)
				{
					try
					{
						Thread.sleep(MarsRuntime.MAIN_CYCLE_TIME - roundDurationTime);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
				}
				
				if (MarsRuntime.SHOW_FPS)
				{
					this.frameDurationTime = System.currentTimeMillis() - this.frameStartTime;
					this.frameCounter++;
					if (this.frameDurationTime > 1000)
					{
						this.frameStartTime = System.currentTimeMillis();
						this.fps = this.frameCounter;
						this.frameCounter = 0;
					}
					drawFPS(canvas);
				}
				
				// 绘制后解锁，绘制后必须解锁才能显示
				mSurfaceHolder.unlockCanvasAndPost(canvas);
			}
			
		}
		
		// }
	}
	
	public void drawFPS(Canvas canvas)
	{

		String FPS = String.valueOf(fps);
		// 绘图
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		// mPaint.setColor(Color.BLACK);
		// canvas.drawRect(0, 0, 200, 50, mPaint);
		mPaint.setColor(Color.RED);
		// canvas.drawText("装在进度："+0+"%......", 10, 20, mPaint);
		canvas.drawText("帧：" + FPS, 10, 20, mPaint);
		
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		System.out.println("widthMeasureSpec == " + widthMeasureSpec);
		System.out.println("heightMeasureSpec == " + heightMeasureSpec);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{

		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
	{

		return super.onKeyMultiple(keyCode, repeatCount, event);
	}
	
	@Override
	public boolean onKeyPreIme(int keyCode, KeyEvent event)
	{

		return super.onKeyPreIme(keyCode, event);
	}
	
	@Override
	public boolean onKeyShortcut(int keyCode, KeyEvent event)
	{

		return super.onKeyShortcut(keyCode, event);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{

		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{

		int iAction = event.getAction();
		float x = event.getX();
		float y = event.getY();
		if (iAction == MotionEvent.ACTION_CANCEL)
		{
			System.out.println("ACTION_CANCEL == " + x + ";" + y);
		}
		else if (iAction == MotionEvent.ACTION_DOWN)
		{
			System.out.println("ACTION_DOWN == " + x + ";" + y);
		}
		else if (iAction == MotionEvent.ACTION_MOVE)
		{
			System.out.println("ACTION_MOVE == " + x + ";" + y);
		}
		else if (iAction == MotionEvent.ACTION_OUTSIDE)
		{
			System.out.println("ACTION_OUTSIDE == " + x + ";" + y);
		}
		else if (iAction == MotionEvent.ACTION_UP)
		{
			System.out.println("ACTION_UP == " + x + ";" + y);
		}
		
		return super.onTouchEvent(event);
	}
	
	@Override
	public boolean onTrackballEvent(MotionEvent event)
	{

		return super.onTrackballEvent(event);
	}
	
}